package com.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 796. 旋转字符串
 * 暴力破解法
 *
 * @author fy
 * @date 2022/4/7 10:04
 */
public class Solution796 {

    public boolean rotateString(String s, String goal) {
        if (s == null || goal == null) {
            return false;
        }
        if (s.length() != goal.length()) {
            return false;
        }
        int n = s.length();

        char[] sArr = s.toCharArray();
        char[] gArr = goal.toCharArray();

        // 找到所有的可能的位移的k
        List<Integer> kList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            char sch = sArr[i];
            if (gArr[0] == sch) {
                kList.add(i);
            }
        }

        // 匹配所有字符是否在位移的位置上
        for (Integer k : kList) {
            boolean flag = true;
            for (int i = 0; i < n; i++) {
                if (sArr[(i + k) % n] != gArr[i]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
//        String s = "abcde";
//        String goal = "cdeab";

//        String s = "bbbacddceeb";
//        String goal = "ceebbbbacdd";

        String s = "abcde";
        String goal = "abced";

        boolean res = new Solution796().rotateString(s, goal);
        System.out.println(res);
    }

}
